package com.xjh.mange.service.Impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjh.mange.mapper.PictureMapper;
import com.xjh.mange.model.Picture;
import com.xjh.mange.service.PictureService;
import com.xjh.mange.vo.PictureDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author 徐佳辉
 * @date 2020/12/20 16:42
 */
@Service
@Slf4j
public class PictureServiceImpl extends ServiceImpl<PictureMapper, Picture> implements PictureService {

    @Autowired
    private PictureMapper pictureMapper;

    @Override
    public String up(MultipartFile file, String picUrl) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID().toString().replace("-", "") + originalFilename.substring(originalFilename.lastIndexOf("."));
        log.info("fileName:[{}]", fileName);
        String realPath = picUrl + fileName;
        log.info("realPath:[{}]", realPath);
        File file1 = new File(realPath);
        if (!file1.getParentFile().exists()) {
            file1.getParentFile().mkdirs();
        }
        file.transferTo(file1);
        return "/pic/" + fileName;
    }


    @Override
    public List<PictureDto> selectAllByUserIdOrName(Page<PictureDto> picsPage, int userId, String name) {
        List<PictureDto> pictureDtoIPage = pictureMapper.selectAllByUserIdOrName(picsPage, userId, name);
        return pictureDtoIPage;
    }
}
